package com.dd.keel.core.common.utils;

import java.math.BigInteger;


public class ValidateUtil {

	
	/**
	 * validate mobile phone number
	 * NOTE: 152********, 153********, 158********, 159********, 13*********
	 * @param mobilePhoneNumber
	 * @return
	 */
	public static boolean isMobilePhoneNumber(String mobilePhoneNumber)
	{
		if(mobilePhoneNumber==null || mobilePhoneNumber.equals("")) return false;
		mobilePhoneNumber = mobilePhoneNumber.trim();
		if(mobilePhoneNumber.startsWith("86"))
		{
			mobilePhoneNumber = mobilePhoneNumber.substring(2);
		}
		if(mobilePhoneNumber.length()!=11) return false;
		/*char tempChar = 0;
		for(int i=0; i<mobilePhoneNumber.length(); ++i)
		{
			tempChar = mobilePhoneNumber.charAt(i);
			if( !(tempChar>='0' && tempChar<='9') ) return false;
		}*/
		if(!RegexUtil.match("13\\d{9}|18\\d{9}|15\\d{9}", mobilePhoneNumber))
		//if(!RegexUtil.match("13\\d{9}|18\\d{9}|15[2|3|8|9]\\d{8}", mobilePhoneNumber))
		{
			return false;
		}
		return true;
	}
	
	public static boolean isIPv4Address(String ipAddress)
	{
		if(ipAddress==null) return false;
		ipAddress = ipAddress.trim();
		if(!RegexUtil.match("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}", ipAddress))
		{
			return false;
		}
		int tempInt = 0;
		String[] partArray = ipAddress.split("\\.");
		for(int i=0; i<partArray.length; ++i)
		{
			tempInt = Integer.parseInt(partArray[i]);
			if( !(tempInt>=0 && tempInt<=255) ) return false;
		}
		return true;
	}
	
	public static boolean belongToIpv4NetRegion(String ipAddress,String startNetRegionAddress,String endNetRegionAddress)
	{
		if( !isIPv4Address(ipAddress) || !isIPv4Address(startNetRegionAddress) || !isIPv4Address(endNetRegionAddress) )
		{
			return false;
		}
		ipAddress = ipAddress.trim();
		startNetRegionAddress = startNetRegionAddress.trim();
		endNetRegionAddress = endNetRegionAddress.trim();
		BigInteger currValue = NetUtil.getLength12NumberByIP4(ipAddress);
		BigInteger minValue = NetUtil.getLength12NumberByIP4(startNetRegionAddress);
		BigInteger maxValue = NetUtil.getLength12NumberByIP4(endNetRegionAddress);
		if(currValue==null || minValue==null || maxValue==null) return false;
		if( (currValue.compareTo(minValue)==1 || currValue.compareTo(minValue)==0) 
			&& (currValue.compareTo(maxValue)==-1 || currValue.compareTo(maxValue)==0) )
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	
}
